package org.zhaijisong.Controller;

import Publics.ResponseResUlt;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zhaijisong.dao.UserDao;
import org.zhaijisong.pojo.User;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/users")
@CrossOrigin(value = "*")
public class UserController {
    @Autowired
    private UserDao userDao;
    @RequestMapping("/listsg")
    @ResponseBody
    public Map<String,Object> list(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "5") int limit) throws Exception{
        Map<String,Object> map = new HashMap<>();
        Page<User> pager = PageHelper.startPage(page,limit);
        List<User> list = userDao.getAll();
        map.put("count",pager.getTotal());
        map.put("data",list);
        map.put("code",0);
        map.put("msg","");
        return map;
    }
    @RequestMapping("/add")
    @ResponseBody
    public ResponseResUlt add(User user){
        userDao.add(user);
        return ResponseResUlt.createSuccessResUle();
    }
}
